<?php

namespace common\widgets\address;

use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use yii\widgets\InputWidget;

/**
 * AddressInput
 *
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class AddressInput extends InputWidget {

    /**
     * @inheritdoc
     */
    public function run() {
        $this->registerAssets();
        echo Html::tag('div', $this->renderInput(), [
            'style' => 'position: relative;width: 100%;',
        ]);
    }

    /**
     * Renders the date picker widget.
     */
    protected function renderInput() {
        $this->options = ArrayHelper::merge([
            'readonly' => 'readonly',
            'data-toggle' => 'city-picker',
            'placeholder' => '请选择 省/市/区'
        ], $this->options);
        return $this->renderInputHtml('hidden');
    }

    /**
     * Registers the needed assets
     */
    public function registerAssets() {
        $view = $this->getView();
        AddressAsset::register($view);
        $scripts = 'jQuery("#' . $this->options['id'] . '").citypicker();';
        $view->registerJs($scripts);
    }

}
